package com.zjut.an.util.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
/**
 * Java使用AES加密算法进行加密解密
 * */
public class AESUtils {
	private static final Logger logger = LoggerFactory.getLogger(AESUtils.class);
	private static String key="20231229swytswyt";
    /**
     * AES算法加密
     * @Param:text原文
     * @Param:key密钥
     * */
    public static String AESEncryptDemo(String text) {
    	String result = "";
    	try {
    		// 创建AES加密算法实例(根据传入指定的秘钥进行加密)
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
     
            // 初始化为加密模式，并将密钥注入到算法中
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
     
            // 将传入的文本加密
            byte[] encrypted = cipher.doFinal(text.getBytes());
     
            //生成密文
            // 将密文进行Base64编码，方便传输
            result = Base64.getEncoder().encodeToString(encrypted);
    	} catch (Exception exception) {
            logger.info("加密失败: {}", exception.getMessage());
            return "";
        }
        return result;
    }
 
    /**
     * AES算法解密
     * @Param:base64Encrypted密文
     * @Param:key密钥
     * */
    public static String AESDecryptDemo(String base64Encrypted) {
    	String result = "";
    	try {
    		// 创建AES解密算法实例
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
     
            // 初始化为解密模式，并将密钥注入到算法中
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
     
            // 将Base64编码的密文解码
            byte[] encrypted = Base64.getDecoder().decode(base64Encrypted);
     
            // 解密
            byte[] decrypted = cipher.doFinal(encrypted);
            result = new String(decrypted);
    	} catch (Exception exception) {
            logger.info("解密失败: {}", exception.getMessage());
            return "";
        }
        return result;

        
    }
   
    public static void main(String[] args) throws Exception {
        //明文
        String text="浙江大学";
        //秘钥(需要使用长度为16、24或32的字节数组作为AES算法的密钥，否则就会遇到java.security.InvalidKeyException异常)
        //加密，生成密文
        String base64Encrypted = AESEncryptDemo(text);
 
        System.out.println(base64Encrypted);
        //解密，获取明文
        String text2 = AESDecryptDemo(base64Encrypted);
 
        System.out.println(text2);
    }
}